﻿Imports Framework.Data

Namespace FileSystemScan
    Friend Class ScanManager
        Public Shared Sub ScanShareForChanges(ByVal share As Entities.Share, ByVal startPath As String)
            Dim blnCompletedWithourErrors As Boolean = False
            Try
                FileShare.Engine.Manager.OnScanShareStarting(share)
                If String.IsNullOrEmpty(startPath) Then
                    FileShare.Engine.Manager.OnLogEvent(String.Format("Rescan of entire share {0} started...", share.Name))
                Else
                    FileShare.Engine.Manager.OnLogEvent(String.Format("Rescan of changes for path {0} started...", startPath))
                End If

                Select Case share.GroupMode
                    Case Entities.GroupMode.GroupSubDirectories
                        Handler_GroupSubDirectories.Scan(share, startPath)
                    Case Entities.GroupMode.GroupIdenticalExtensionlessFilenames
                        Handler_GroupIndenticalFilenames.Scan(share, startPath)
                    Case Entities.GroupMode.IndividualFiles
                        Handler_GroupIndividualFiles.Scan(share, startPath)
                    Case Else
                        Throw New NotImplementedException
                End Select

                'handle orphaned files
                HandleOrphanedFiles()
                blnCompletedWithourErrors = True
            Finally
                FileShare.Engine.Manager.OnScanShareCompleted(share)
                If blnCompletedWithourErrors Then
                    If String.IsNullOrEmpty(startPath) Then
                        FileShare.Engine.Manager.OnLogEvent(String.Format("Rescan of share {0} completed", share.Name))
                    Else
                        FileShare.Engine.Manager.OnLogEvent(String.Format("Rescan of changes for path {0} completed", startPath))
                    End If
                Else
                    If String.IsNullOrEmpty(startPath) Then
                        FileShare.Engine.Manager.OnLogEvent(String.Format("Rescan of share {0} aborted due to errors", share.Name))
                    Else
                        FileShare.Engine.Manager.OnLogEvent(String.Format("Rescan of changes for path {0} aborted due to errors", startPath))
                    End If
                End If
            End Try
        End Sub

        Private Shared Sub HandleOrphanedFiles()
            Using t As New TransactionScope
                EntityHandlers.SharedItemsFiles.ReevalOrphanedSince(t)

                EntityHandlers.SharedItemsFiles.RemoveOldOrphans(Now.Subtract(TimeSpan.FromDays(180)), t)
                t.Complete()
            End Using
        End Sub

        'Private Shared Sub CreateOrUpdateMissingMediaInfo(ByVal tracker As ScanTracker)
        '    'check and make sure that there are no movies without a current movieinfo
        '    Dim aItems As Generic.List(Of Entities.SharedItem) = EntityHandlers.SharedItems.GetAllMoviesWithMissingOrOutDatedMediaInfo
        '    For Each clsItem As Entities.SharedItem In aItems
        '        Dim clsMI As Entities.MediaInfo
        '        clsMI = ScanFolder_Common.CreateMediaInfoEx(clsItem)
        '        If clsMI IsNot Nothing Then
        '            Using t As New TransactionScope
        '                DataStore.MediaInfo.Save(clsMI, t)
        '                clsItem.HasMediaInfo = True
        '                EntityHandlers.SharedItems.Save(clsItem, t)
        '                t.Complete()
        '            End Using
        '            FileShare.UI.Manager.OnSharedItemUpdated(clsItem)
        '        End If
        '    Next
        'End Sub

    End Class

End Namespace